﻿using NodeCanvas.BehaviourTrees;
using NodeCanvas.Framework;
using ParadoxNotion.Design;
using Snake.Misc;
using UnityEngine;

namespace Snake.Gameplay.GameplayBehaviourTreeExtension
{
    [Category("Gameplay Nodes")]
    [ParadoxNotion.Design.Icon("Condition")]
    [Description("Find target")]
    public class FindTarget : BTNode
    {
        /// <summary>
        /// RadiusX
        /// </summary>
        public float radius;

        protected override Status OnExecute(Component agent, IBlackboard blackboard)
        {
            var asc = blackboard.GetVariableValue<GameplayAbilityComponent>("ASC");
            Utils.OverlapEllipseNonAlloc(agent.transform.position, radius, radius * 0.5f, out var results);
            foreach (var col in results)
            {
                if (!col.TryGetComponent(out GameplayAbilityComponent com) || com == asc)
                {
                    continue;
                }

                blackboard.SetVariableValue("Target", com.gameObject);
            }

            return results.Count > 0 ? Status.Success : Status.Running;
        }
    }
}